package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.commom.exception.CustException;
import com.heima.commom.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class ChannelSServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public PageResponseResult findByQuery(ChannelDTO dto) {
        // 1. 参数校验代码
        dto.checkParam();
        // 2. 分页查询
        // 2.1 封装分页查询条件
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        // 2.2 其他查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) { //true 不为空
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {
            queryWrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        // 2.3 按照ord 升序排序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        // 2.3 执行查询操作   参数1: 分页条件  参数2： 查询条件
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        // 封装成 PageResponseResult结果返回
        return new PageResponseResult(dto.getPage(), dto.getSize(),
                pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        // 1. 参数校验代码
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能大于10个字符");
        }
        // 判断频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已经存在");
        }
        // 新增
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        // 返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1 参数检验
        Integer id = adChannel.getId();
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空");
        }
        // 2 检查数据是否存在
        AdChannel oldChannel = getById(adChannel.getId());
        if (oldChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在");
        }
        // 名字不为空且名字与原来不一致,发生改变，需校验
        String name = adChannel.getName();
        if (StringUtils.isNotBlank(name) && !name.equals(oldChannel.getName())) {
            if (name.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能大于10个字符");
            }
            // 判断频道是否存在
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已经存在");
            }
        }
        // 修改频道
        updateById(adChannel);
        // 返回结果
        return ResponseResult.okResult();
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public ResponseResult delete(Integer id) {
//         模拟异常
        if (id == 1) {
//            System.out.println(1/0);
//            throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW, "id不能为1");
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"参数不正确");
        }
        //1 参数检验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空");
        }
        // 判断频道是否存在
        AdChannel adChannel = getById(id);
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在");
        }
        // 查看状态
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW, "启用状态不能删除");
        }
        // 删除频道
        removeById(id);
        // 返回结果
        return ResponseResult.okResult();
    }
}
